home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / graphics / 3dview12.zip / 3DFUNC.CPP < prev    next >
C/C++ Source or Header  |  1996-05-28  |  15KB  |  481 lines

  1. #include <stdio.h>
  2. #include <bios.h>
  3. #include <time.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6. #include <i86.h>
  7. #include <string.h>
  8.  
  9. #include "defines.hpp"
  10.  
  11. #include "vesa.hpp"
  12. #include "3d.hpp"
  13. #include "vesa3d.hpp"
  14. #include "misc.hpp"
  15. #include "mouse.hpp"
  16.  
  17. typedef double T3DFunction(double, double);
  18.  
  19. void ShowFunction( T3DFunction f, double X1, double Y1, double X2, double Y2,
  20.                    long m, long n, DWORD PalStart ) {
  21.     long* fPoints = new long [(m+1)*(n+1)];
  22.     double DX = (X2-X1)/n;
  23.     double DY = (Y2-Y1)/m;
  24.  
  25.     for ( long i = 0; i <= m; i++ ) {
  26.         for ( long j = 0; j <= n; j++ ) {
  27.             fPoints[i*(n+1)+j] = _3D_NewPoint( X1+j*DX, Y1+i*DY, f(X1+j*DX, Y1+i*DY) );
  28.         };
  29.     };
  30.  
  31.     for ( i = 0; i < m; i++ ) {
  32.         for ( long j = 0; j < n; j++ ) {
  33.             VESA3D_New3DQuad( fPoints[i*(n+1)+j], fPoints[i*(n+1)+j+1],
  34.                               fPoints[(i+1)*(n+1)+j+1], fPoints[(i+1)*(n+1)+j],
  35.                               PalStart, Doublesided, GouraudShadedZBuf );
  36.         };
  37.     };
  38. };
  39.  
  40. void ShowFunction_Tex( T3DFunction f, double X1, double Y1, double X2, double Y2,
  41.                         long m, long n, VESA3D_Texture &TEX ) {
  42.     long* fPoints = new long [(m+1)*(n+1)];
  43.     double DX = (X2-X1)/n;
  44.     double DY = (Y2-Y1)/m;
  45.     double TEXDX = double(TEX.TDX) / n;
  46.     double TEXDY = double(TEX.TDY) / m;
  47.  
  48.     for ( long i = 0; i <= m; i++ ) {
  49.         for ( long j = 0; j <= n; j++ ) {
  50.             fPoints[i*(n+1)+j] = _3D_NewPoint( X1+j*DX, Y1+i*DY, f(X1+j*DX, Y1+i*DY) );
  51.         };
  52.     };
  53.  
  54.     for ( i = 0; i < m; i++ ) {
  55.         for ( long j = 0; j < n; j++ ) {
  56.             _3D_NewObject(
  57.              new _3D_Triangle( fPoints[i*(n+1)+j], j*TEXDX, i*TEXDY,
  58.                                 fPoints[i*(n+1)+j+1], (j+1)*TEXDX, i*TEXDY,
  59.                                 fPoints[(i+1)*(n+1)+j+1], (j+1)*TEXDX, (i+1)*TEXDY,
  60.                                 &TEX, Doublesided, Rectangle, TexturedZBuf ));
  61.             _3D_NewObject(
  62.              new _3D_Triangle( fPoints[(i+1)*(n+1)+j+1], (j+1)*TEXDX, (i+1)*TEXDY,
  63.                                 fPoints[(i+1)*(n+1)+j], j*TEXDX, (i+1)*TEXDY,
  64.                                 fPoints[i*(n+1)+j], j*TEXDX, i*TEXDY,
  65.                                 &TEX, Doublesided, Rectangle, TexturedZBuf ));
  66.         };
  67.     };
  68. };
  69.  
  70. void ShowTitle() {
  71.     printf( "# 3D-BMP-Viewer V1.2   (w) by P.Kayser 1996 \n" );
  72.     printf( "# EMAIL : p.kayser@tu-bs.de\n" );
  73. };
  74.  
  75. void ShowHeader() {
  76.     ShowTitle();
  77.     printf( "# usage : 3DVIEW (-abdmz) <File(.BMP)> (<Mode>)\n" );
  78.     printf( "# \n" );
  79.     printf( "# options\n" );
  80.     printf( "#   -a        : show additional informations.\n" );
  81.     printf( "#   -b        : disable bitmap.\n" );
  82.     printf( "#   -d<dist>  : distance. <dist> from 0.01 (far) to 3 (near).\n" );
  83.     printf( "#   -m        : enable mouse-control.\n" );
  84.     printf( "#   -z        : enable z-buffer.\n" );
  85. };
  86.  
  87. void ShowAddInfo() {
  88.     printf( "# \n" );
  89.     printf( "# Needs VBE version 2.0.\n" );
  90.     printf( "# Only 256-color-modes allowed.\n" );
  91.     printf( "# The bitmap must be 'Windows-RGB-Encoded' and should have\n" );
  92.     printf( "# a maximum of 192 colors. Colors 192..255 are used for the\n" );
  93.     printf( "# border.\n" );
  94.     printf( "# \n" );
  95.     printf( "# 256-Color-VBE-Modes :\n" );
  96.     printf( "# 640x480 : 101 | 800x600 : 103 | 1024x768 : 105\n" );
  97. };
  98.  
  99. double Phi = 0;
  100.  
  101. double G( double x, double y ) {
  102.     double g = 1-2*x*x-4*y*y;
  103.     if ( g >= 0 )
  104.         return sqrt(g);
  105.     else
  106.         return 0;
  107. };
  108.  
  109. double J( double x, double y ) {
  110.     double g = 1-2*x*x-4*y*y;
  111.     if ( g >= 0 )
  112.         return -sqrt(g);
  113.     else
  114.         return 0;
  115. };
  116.  
  117. double H( double x, double y ) {
  118.     double h = 1-x;
  119.     return h;
  120. };
  121.  
  122. void PF( _3D_Point &P ) {
  123.     P.Z = G(P.X,P.Y);
  124. };
  125.  
  126. void main( int argc, char *argv[] ) {
  127.  
  128.     Boolean ShowBitmap = True;
  129.     Boolean ShowAddInfos = False;
  130.     Boolean WithZBuffer = False;
  131.     Boolean MouseControl = False;
  132.     double Distance = 1;
  133.     int CmdCount = 1;
  134.     char Buffer [15];
  135.  
  136.     double MinZoom = 0.01;
  137.     double MaxZoom = 3;
  138.     double NormZoom = 1;
  139.  
  140.     if ( argc == CmdCount ) {
  141.         VESA_SetTextMode();
  142.         ShowHeader();
  143.         return;
  144.     };
  145.  
  146.     while ( (argv[CmdCount])[0] == '-' ) {
  147.         int OptCount = 1;
  148.         while ( OptCount < strlen( argv[CmdCount] ) ) {
  149.             switch ( (argv[CmdCount])[OptCount] ) {
  150.                 case 'a' :
  151.                     ShowAddInfos = True;
  152.                     break;
  153.                 case 'b' :
  154.                     ShowBitmap = False;
  155.                     break;
  156.                 case 'd' :
  157.                     Distance = strtod( strncpy( Buffer,
  158.                                                 &(argv[CmdCount])[OptCount+1],
  159.                                                 strlen( argv[CmdCount] ) - OptCount ),
  160.                                        NULL );
  161.                     if ( Distance < MinZoom || Distance > MaxZoom )
  162.                         Distance = NormZoom;
  163.                     OptCount = strlen ( argv[CmdCount] );                                       
  164.                     break;
  165.                 case 'z' :
  166.                     WithZBuffer = True;
  167.                     break;
  168.                 case 'm' :
  169.                     MouseControl = True;
  170.                     break;
  171.                 default:
  172.                     VESA_SetTextMode();
  173.                     ShowTitle();
  174.                     printf( "\n" );
  175.                     printf( "ERROR : '%c' is an unknown option !\n" , argv[CmdCount][OptCount] );
  176.                     return;
  177.             }
  178.             OptCount++;
  179.         };
  180.         CmdCount++;
  181.     };
  182.  
  183.     if ( ShowAddInfos == True ) {
  184.         VESA_SetTextMode();
  185.         ShowHeader();
  186.         ShowAddInfo();
  187.         return;       
  188.     };
  189.  
  190.     static VESA3D_Texture TEX;
  191.     char* Filename = new char [256];
  192.     strcpy( Filename, argv[CmdCount] );
  193.  
  194.     if ( VESA3D_LoadTexture( Filename, TEX ) != 0 )
  195.         if ( VESA3D_LoadTexture( strcat( Filename, ".BMP" ), TEX ) != 0 ) {
  196.             VESA_SetTextMode();
  197.             ShowTitle();
  198.             printf( "\n" );
  199.             printf( "ERROR : Could not open .BMP-File !\n" );
  200.             return;
  201.     };
  202.  
  203.     VESA3D_ConvertTexture( TEX );
  204.  
  205.     CmdCount++;
  206.  
  207.     WORD Mode;
  208.     
  209.     if ( argc == CmdCount ) {
  210.         Mode = 0x0101;
  211.     }
  212.     else {
  213.         Mode = WORD( strtoul( argv[CmdCount], NULL, 16 ) );
  214.     };
  215.  
  216.     VESA_SetTextMode();
  217.     ShowTitle();
  218.     
  219.     if ( VESA_InitMode( Mode ) != 0 ) {
  220.         return;
  221.     };
  222.  
  223.     if ( VESA_BpP != 8 ) {
  224.         VESA_SetTextMode();
  225.         ShowTitle();
  226.         printf( "\n" );
  227.         printf( "ERROR: Not a 256-Color-Mode !\n" );
  228.         return;
  229.     };
  230.  
  231.     VESA3D_Init();
  232.     if ( WithZBuffer == True )
  233.         VESA3D_MakeZBuffer();
  234.  
  235.     /*
  236.     for ( int i = 47; i >= 0; i-- )
  237.         VESA_SetColor8B( WORD(239-i) ,
  238.                          WORD(32+i*5/2),
  239.                          WORD(32+i*5/2),
  240.                          WORD(32+i*5/2) );
  241.  
  242.     for ( i = 15; i >= 0; i-- )
  243.         VESA_SetColor8B( WORD(255-i),
  244.                          WORD(128+i*8),
  245.                          WORD(128+i*8),
  246.                          WORD(128+i*8) );
  247.  
  248.     VESA_SetColor8B( 0, 0, 0, 0 );
  249.  
  250.     if ( ShowBitmap == True )
  251.         VESA3D_SetTexturePalette( TEX, 192 );
  252.  
  253.     double px, py;
  254.  
  255.     if ( TEX.TDY > TEX.TDX ) {
  256.         px = 1;
  257.         py = double(TEX.TDY) / TEX.TDX;
  258.     }
  259.     else {
  260.         py = 1;
  261.         px = double(TEX.TDX) / TEX.TDY;
  262.     };
  263.  
  264.     long p1 = _3D_NewPoint(  0, py, 0 );
  265.     long p2 = _3D_NewPoint( px, py, 0 );
  266.     long p3 = _3D_NewPoint( px,  0, 0 );
  267.     long p4 = _3D_NewPoint(  0,  0, 0 );
  268.  
  269.     double dx = 0.25, dz = 0.06;
  270.  
  271.     long p5  = _3D_NewPoint( -dx, -dx, dz );
  272.     long p6  = _3D_NewPoint( -dx, -dx, -dz );
  273.     long p7  = _3D_NewPoint( px+dx, -dx, dz );
  274.     long p8  = _3D_NewPoint( px+dx, -dx, -dz );
  275.     long p9  = _3D_NewPoint( -dx, py+dx, dz );
  276.     long p10 = _3D_NewPoint( -dx, py+dx, -dz );
  277.     long p11 = _3D_NewPoint( px+dx, py+dx, dz );
  278.     long p12 = _3D_NewPoint( px+dx, py+dx, -dz );
  279.  
  280.     long p13 = _3D_NewPoint( 0, 0, dz );
  281.     long p14 = _3D_NewPoint( 0, 0, -dz );
  282.     long p15 = _3D_NewPoint( px, 0, dz );
  283.     long p16 = _3D_NewPoint( px, 0, -dz );
  284.     long p17 = _3D_NewPoint( 0, py, dz );
  285.     long p18 = _3D_NewPoint( 0, py, -dz );
  286.     long p19 = _3D_NewPoint( px, py, dz );
  287.     long p20 = _3D_NewPoint( px, py, -dz );
  288.  
  289.     double ds = 0.2;
  290.  
  291.     long p21 = _3D_NewPoint( px/2-ds, py/2+ds, ds );
  292.     long p22 = _3D_NewPoint( px/2+ds, py/2+ds, ds );
  293.     long p23 = _3D_NewPoint( px/2+ds, py/2+ds, -ds );
  294.     long p24 = _3D_NewPoint( px/2-ds, py/2+ds, -ds );
  295.     long p25 = _3D_NewPoint( px/2-ds, py/2-ds, ds );
  296.     long p26 = _3D_NewPoint( px/2+ds, py/2-ds, ds );
  297.     long p27 = _3D_NewPoint( px/2+ds, py/2-ds, -ds );
  298.     long p28 = _3D_NewPoint( px/2-ds, py/2-ds, -ds );
  299.  
  300.     DWORD PalStart = 192;
  301.  
  302.     _3D_Triangle_SideTyp S = Singlesided;
  303.     _3D_Triangle_Typ T;
  304.     if ( WithZBuffer == True )
  305.         T = GouraudShadedZBuf;
  306.     else
  307.         T = GouraudShaded;
  308.  
  309.     if ( ShowBitmap == True )
  310.         if ( WithZBuffer == True )
  311.             VESA3D_New3DTexQuad (  p1,  p2,  p3,  p4, TEX, Doublesided, TexturedZBuf );
  312.         else
  313.             VESA3D_New3DTexQuad (  p1,  p2,  p3,  p4, TEX, Doublesided, Textured );
  314.             
  315.     VESA3D_New3DQuad(  p5,  p7,  p8,  p6, PalStart, S, T ); 
  316.     VESA3D_New3DQuad(  p9,  p5,  p6, p10, PalStart, S, T ); 
  317.     VESA3D_New3DQuad( p11,  p9, p10, p12, PalStart, S, T );
  318.     VESA3D_New3DQuad(  p7, p11, p12,  p8, PalStart, S, T );
  319.     VESA3D_New3DQuad(  p9, p17, p13,  p5, PalStart, S, T );
  320.     VESA3D_New3DQuad(  p6, p14, p18, p10, PalStart, S, T );
  321.     VESA3D_New3DQuad(  p9, p11, p19, p17, PalStart, S, T );
  322.     VESA3D_New3DQuad( p18, p20, p12, p10, PalStart, S, T );
  323.     VESA3D_New3DQuad( p19, p11,  p7, p15, PalStart, S, T );
  324.     VESA3D_New3DQuad( p16,  p8, p12, p20, PalStart, S, T );
  325.     VESA3D_New3DQuad( p13, p15,  p7,  p5, PalStart, S, T );
  326.     VESA3D_New3DQuad(  p6,  p8, p16, p14, PalStart, S, T );
  327.     VESA3D_New3DQuad( p17, p19,  p2,  p1, PalStart, S, T );
  328.     VESA3D_New3DQuad( p13, p17,  p1,  p4, PalStart, S, T );
  329.     VESA3D_New3DQuad( p19, p15,  p3,  p2, PalStart, S, T );
  330.     VESA3D_New3DQuad( p15, p13,  p4,  p3, PalStart, S, T );
  331.     VESA3D_New3DQuad(  p1,  p2, p20, p18, PalStart, S, T );
  332.     VESA3D_New3DQuad(  p2,  p3, p16, p20, PalStart, S, T );
  333.     VESA3D_New3DQuad(  p3,  p4, p14, p16, PalStart, S, T );
  334.     VESA3D_New3DQuad(  p4,  p1, p18, p14, PalStart, S, T );
  335.  
  336.     
  337.     if ( WithZBuffer == True ) {
  338.         VESA3D_New3DQuad( p24, p23, p22, p21, PalStart, Singlesided, GouraudShadedZBuf );
  339.         VESA3D_New3DQuad( p21, p22, p26, p25, PalStart, Singlesided, GouraudShadedZBuf );
  340.         VESA3D_New3DQuad( p22, p23, p27, p26, PalStart, Singlesided, GouraudShadedZBuf );
  341.         VESA3D_New3DQuad( p23, p24, p28, p27, PalStart, Singlesided, GouraudShadedZBuf );
  342.         VESA3D_New3DQuad( p24, p21, p25, p28, PalStart, Singlesided, GouraudShadedZBuf );
  343.         VESA3D_New3DQuad( p25, p26, p27, p28, PalStart, Singlesided, GouraudShadedZBuf );
  344.     };
  345.  
  346.     double MaxDist = sqrt( (px/2+dx)*(px/2+dx) + (py/2+dx)*(py/2+dx) + dz*dz );
  347.  
  348.     if ( px > py ) {
  349.         Rad = 3 * px;
  350.     }
  351.     else {
  352.         Rad = 3 * py ;
  353.     };
  354.  
  355.     
  356.     */
  357.  
  358.     for ( int i = 127; i >= 0; i-- )
  359.        VESA_SetColor8B( WORD(255-i) ,
  360.                         0,
  361.                         WORD(128+i),
  362.                         WORD(128+i) ); 
  363.  
  364.     for ( i = 126; i >= 0; i-- )
  365.        VESA_SetColor8B( WORD(127-i) ,
  366.                         WORD(128+i),
  367.                         0,
  368.                         WORD(128+i) ); 
  369.  
  370.     // if ( ShowBitmap == True )
  371.     //    VESA3D_SetTexturePalette( TEX, 192 );
  372.  
  373.     double px = 0;
  374.     double py = 0;
  375.  
  376.     ShowFunction( G, -1, 1, 1, -1, 15, 15, 0 );
  377.     //ShowFunction( J, -2, 2, 2, -2, 15, 15, 0 );
  378.     ShowFunction( H, -1, 0.5, 1, -0.5, 1, 1, 128 );
  379.     // ShowFunction_Tex( G, -3, 3, 3, -3, 15, 15, TEX );
  380.  
  381.     double Rad = 10;
  382.  
  383.     double MaxDist = 2;
  384.  
  385.     int N = 0;
  386.     VESA3D_ShadeSub = Rad - MaxDist;
  387.     VESA3D_ShadeMul = 128 / ( 2 * MaxDist );
  388.     
  389.     long *StarsX = new long [300];
  390.     long *StarsY = new long [300];
  391.     char *StarsC = new char [300];
  392.  
  393.     _3D_SetAlpha( 1.57 );
  394.     _3D_SetPhi( 0 );
  395.     _3D_SetGamma( 0 );
  396.     _3D_SetOmega( 0 );
  397.     _3D_SetEye( 0, 0, 0 );
  398.  
  399.     for ( i = 0; i < 300; i++ ) {
  400.         StarsX[i] = MISC_Random(VESA_MaxX);
  401.         StarsY[i] = MISC_Random(VESA_MaxY);
  402.         StarsC[i] = char( 240+MISC_Random(15) );
  403.     };
  404.  
  405.     double A = 0, B = 1.57, C = 0, D = 0;
  406.  
  407.     _3D_ScreenDistance = Distance;
  408.  
  409.     _3D_Matrix SpecMatrix;
  410.     _3D_InitMatrix_Rotate( SpecMatrix, sin(0.1), cos(0.1), sin(0.1), cos(0.1),
  411.                            0, 1 ); 
  412.  
  413.     MOUSE_SMouseMoves Move;
  414.     MOUSE_SMouseButtons Buttons;
  415.  
  416.     clock_t Start = clock();
  417.  
  418.     while( _bios_keybrd( _KEYBRD_READY )==0 ) {
  419.         N++;
  420.         Phi+=0.05;
  421.         //_3D_ForAllPointsDo(PF);
  422.         //if ( WithZBuffer == True )
  423.             //_3D_RotatePointsForward( p21, p28, px/2, py/2, 0, SpecMatrix );
  424.          if ( MouseControl == True ) {
  425.             MOUSE_GetMouseMoves( Move );
  426.             MOUSE_GetButtonState( Buttons );
  427.             A += double(Move.MoveDX) / 100;
  428.             B += double(Move.MoveDY) / 100;
  429.             if ( Buttons.RightButton == True ) {
  430.                 _3D_ScreenDistance -= 0.1;
  431.                 if ( _3D_ScreenDistance < MinZoom )
  432.                     _3D_ScreenDistance = MinZoom;
  433.             };
  434.             if ( Buttons.LeftButton == True ) {
  435.                 _3D_ScreenDistance += 0.1;
  436.                 if ( _3D_ScreenDistance > MaxZoom )
  437.                     _3D_ScreenDistance = MaxZoom;
  438.             };
  439.         } 
  440.         else { 
  441.             D += 0.1; 
  442.             A += 0.02 + 0.02 * sin( A + D );
  443.             B += 0.04 + 0.04 * sin( B + D );
  444.             C += 0.06 + 0.06 * sin( C + D );
  445.         };
  446.         _3D_SetPhi( A );
  447.         _3D_SetGamma( B );
  448.         _3D_SetOmega( C );
  449.         _3D_SetEye_Globe( px/2, py/2, 0, Rad );
  450.         _3D_InitRotation();
  451.         _3D_ProjectPoints();
  452.         _3D_PrepareObjects();
  453.         if ( WithZBuffer == True ) {
  454.             VESA3D_ClearZBuffer();
  455.             // _3D_SortObjectsZ();
  456.         }
  457.         else
  458.             _3D_SortObjects();
  459.         VESA3D_FlipPageWrite();
  460.         VESA_ClearScreen8B( 0, VESA3D_WriteSelector );
  461.         for ( i = 0; i < 300; i++ ) {
  462.             VESA_Pix8BC( StarsX[i], StarsY[i], StarsC[i], VESA3D_WriteSelector );
  463.         };
  464.         _3D_DrawObjects();
  465.         VESA3D_FlipPageShow();
  466.         //VESA_SyncDisplay();
  467.     };
  468.  
  469.     clock_t End = clock();
  470.            
  471.     _bios_keybrd( _KEYBRD_READ );
  472.  
  473.     VESA_SetTextMode();
  474.     ShowTitle();
  475.     printf( "\n" );
  476.     printf( "%f Frames per second.\n", double( N ) / ( End-Start ) * CLOCKS_PER_SEC );
  477.  
  478.     VESA_Exit(); 
  479. };
  480.  
  481.